package com.changgou.goods.service.impl;

import com.changgou.entity.Page;
import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.dao.CategoryMapper;
import com.changgou.goods.pojo.Category;
import com.changgou.goods.service.BrandService;
import com.changgou.goods.pojo.Brand;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.StringUtil;

import java.util.List;
import java.util.Map;

@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    private BrandMapper brandMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Map> findByCategory(String name) {
        List<Map> findBrandListByCategoryName = brandMapper.findBrandListByCategoryName(name);
        return findBrandListByCategoryName;
    }

    @Override
    public List<Brand> findByCategoryId(Integer id) {
       return brandMapper.findByCategoryId(id);
    }

    /**
     * 查询全部列表
     * @return
     */
    @Override
    public List<Brand> findAll() {
        return brandMapper.selectAll();
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public Brand findById(Integer id){
        return  brandMapper.selectByPrimaryKey(id);
    }


    /**
     * 增加
     * @param brand
     */
    @Override
    public void add(Brand brand){
        brandMapper.insert(brand);
    }


    /**
     * 修改
     * @param brand
     */
    @Override
    public void update(Brand brand){
        brandMapper.updateByPrimaryKey(brand);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        brandMapper.deleteByPrimaryKey(id);
    }

    /**
     * 条件查询
     * @param brand
     * @return
     */
    @Override
    public List<Brand> findList(Brand brand) {
        Example example = createExample(brand);


        return brandMapper.selectByExample(example);
    }

    /**
     * 分页
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page,size);

        return new Page<Brand>(brandMapper.selectAll());
    }
    /**
     * 分页+条件
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Brand> findPage(Brand brand,int page, int size) {
        PageHelper.startPage(page,size);

        Example example = createExample(brand);

        return new Page<Brand>(brandMapper.selectByExample(example));
    }



    public Example createExample(Brand brand){
        Example example = new Example(Brand.class);
        Example.Criteria criteria = example.createCriteria();
        
        if(brand!=null){
            //品牌名称
            if(StringUtils.isEmpty(brand.getName())){
                criteria.andLike("name","%"+brand.getName()+"%");
            }
            if(StringUtils.isEmpty(brand.getLetter())){
                criteria.andLike("image","%"+brand.getLetter()+"%");
            }
            // 品牌的首字母
            if(!StringUtils.isEmpty(brand.getLetter())){
                criteria.andLike("letter","%"+brand.getLetter()+"%");
            }
            // 品牌id
            if(!StringUtils.isEmpty(brand.getLetter())){
                criteria.andEqualTo("id",brand.getId());
            }
            // 排序
            if(brand.getSeq()!=null){
                criteria.andEqualTo("seq",brand.getSeq());
            }
        }
        return example;
    }


}
